package cmarter.dev.chpt2;

import java.util.Arrays;
import java.util.List;

import cmarter.dev.chpt2.interfaces.AppleConsumer;
import cmarter.dev.chpt2.interfaces.ApplePredicate;
import cmarter.dev.chpt2.predicates.AppleColorPredicate;
import cmarter.dev.chpt2.predicates.ApplePrintPredicate;
import cmarter.dev.chpt2.predicates.ApplePrinter;
import cmarter.dev.chpt2.predicates.AppleWeightPredicate;
import cmarter.dev.chpt2.predicates.AppleWeightResetPredicate;
import cmarter.dev.entities.Apple;

public class PassingCodeBehaviorParameterization {

	public static void main(String[] args) {
		List<Apple> apples = Arrays.asList(new Apple("green", 90d, "Granny Smith"),
										   new Apple("green", 150d, "Granny Smith"),
										   new Apple("green", 190d, "smith"),
										   new Apple("green", 149d, "smith"),
										   new Apple("green", 160d, "smith"),
										   new Apple("red", 175d, "Fuji"),
		   								   new Apple("red", 80d, "Fuji"), 
		   								   new Apple("red", 110d, "Fuji"),
										   new Apple("yellow", 80d, "Fuji"), 
										   new Apple("yellow", 155d, "Fuji"));

		//////////////////////////////////////////////////////////////////////////////////////////////
		List<Apple> greenApples = FarmingInventory.filterApples(apples, new AppleColorPredicate("green"));
		printAppleList(greenApples);
		List<Apple> redApples = FarmingInventory.filterApples(apples, new AppleColorPredicate("red"));
		printAppleList(redApples);
		List<Apple> yellowApples = FarmingInventory.filterApples(apples, new AppleColorPredicate("yellow"));
		printAppleList(yellowApples);
		
		//////////////////////////////////////////////////////////////////////////////////////////////
		List<Apple> heavyApples = FarmingInventory.filterApples(apples, new AppleWeightPredicate(150));
		printAppleList(heavyApples);
		
		//////////////////////////////////////////////////////////////////////////////////////////////
		foreachApple(apples, new ApplePrintPredicate());
		System.out.println();
		
		//////////////////////////////////////////////////////////////////////////////////////////////
		foreachApple(apples, new AppleWeightResetPredicate());
		foreachApple(apples, new ApplePrintPredicate());
		System.out.println();		
		

		//////////////////////////////////////////////////////////////////////////////////////////////
		// THIS WORKS BUT THE ANSWER IS TO CREATE ANOTHER PREDICATE
		// INTERFACE THAT RETURN VOID LIKE SO ...		
		//////////////////////////////////////////////////////////////////////////////////////////////
		foreachApple(apples, new AppleWeightResetPredicate());
		foreachApple(apples, new ApplePrinter());
		System.out.println();
		
		
		

		foreachApple(apples, new AppleColorPredicate() {
			@Override
			public boolean test(Apple a ) {
				System.out.println("COLOR PREDICATE:" + a);
				return "red".equals(a.getColor());
			}
		});
	}

	public static void foreachApple(List<Apple> apples, AppleConsumer p) {
		for (Apple a : apples) {
			p.accept(a);
		}
	}

	public static void foreachApple(List<Apple> apples, ApplePredicate p) {
		for (Apple a : apples) {
			p.test(a);
		}
	}

	private static void printAppleList(List<Apple> apples) {
		for (Apple a : apples) {
			System.out.println(a);
		}
	}		

}


